package com.example.yubo.menudialogtest_403yb;

import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.TextureView;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import static com.example.yubo.menudialogtest_403yb.R.id.editText;

public class MainActivity extends AppCompatActivity {

    private TextView tvinfo,tvmenu;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //获取控件  定义一个init方法 在方法里边获取控件
        init();



        }
    private void init(){
        tvinfo= (TextView) findViewById(R.id.textView);
        tvmenu= (TextView) findViewById(R.id.textView2);
    }
   //创建选项菜单和子菜单
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.main,menu);

        return true;

    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {



        switch (item.getItemId()){
            case R.id.menu1:
                tvinfo.setText("菜单1");
                showdialog();
                break;

            case R.id.menu2:
                tvinfo.setText("菜单2");
                break;

            case R.id.menu3:
                tvinfo.setText("菜单3");
                break;

            case R.id.menu4:
                tvinfo.setText("菜单4");
                break;

            case R.id.menu5:
                tvinfo.setText("菜单5");
                break;

            case R.id.menu6:
                tvinfo.setText("菜单6");
                break;


        }
        return super.onOptionsItemSelected(item);
    }

    private void showdialog() {
        //动态家在布局
        final View view = getLayoutInflater().inflate(R.layout.dialoglayout,null);
        AlertDialog dialog=new AlertDialog.Builder(this)//---------------1
        .setTitle("对话框标题")//-----------------2
        .setIcon(R.mipmap.ic_launcher)//---------3
        .setView(view)//-------------------4
        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                //确定按钮的操作
                EditText et= (EditText) view.findViewById(R.id.editText);
                String str=et.getText().toString();
                tvinfo.setText(str);
            }
        })
                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        //取消 空着就可以
                    }
                })
                .create();//5----创建
        dialog.show();//---------6显示
    }
}
